class template
<functional>
std::binary_negate
template <class Predicate> class binary_negate;
Negate binary function object class
Binary function object class whose call returns the opposite of another binary function, passed to its constructor.
Object of type binary_negate are generally constructed using function not2 .
The class is defined with the same behavior as:
1
2
3
4
5
6
7
8
9
10
11
12
template <class Predicate> class binary_negate
: public binary_function <typename Predicate::first_argument_type,
typename Predicate::second_argument_type, bool>
{
protected:
Predicate fn_;
public:
explicit binary_negate ( const Predicate& pred ) : fn_ (pred) {}
bool operator() (const typename Predicate::first_argument_type& x,
const typename Predicate::second_argument_type& y) const
{ return !fn_(x,y); }
};
1
2
3
4
5
6
7
8
9
10
11
template <class Predicate> class binary_negate
{
protected:
Predicate fn_;
public:
explicit binary_negate (const Predicate& pred) : fn_ (pred) {}
bool operator() (const typename Predicate::argument_type& x) const {return !fn_(x,y);}
typedef typename Predicate::first_argument_type first_argument_type;
typedef typename Predicate::second_argument_type second_argument_type;
typedef bool result_type;
};
Template parameters
- Predicate
- A binary function object class, with members first_argument_type and second_argument_type defined.
Member types
| member type | definition | notes |
| first_argument_type | T | Type of the first argument in member operator() |
| second_argument_type | T | Type of the second argument in member operator() |
| result_type | T | Type returned by member operator() |
Member functions
- constructor
- Constructs an object whose functional call returns the opposite as the object passed as its argument.
- operator()
- Member function returning the opposite of the function object with which the object was constructed.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// binary_negate example
#include <iostream> // std::cout
#include <functional> // std::binary_negate, std::equal_to
#include <algorithm> // std::mismatch
#include <utility> // std::pair
int main () {
std::equal_to<int> equality;
std::binary_negate < std::equal_to<int> > nonequality (equality);
int foo[] = {10,20,30,40,50};
int bar[] = {0,15,30,45,60};
std::pair<int*,int*> firstmatch,firstmismatch;
firstmismatch = std::mismatch (foo,foo+5,bar,equality);
firstmatch = std::mismatch (foo,foo+5,bar,nonequality);
std::cout << "First mismatch in bar is " << *firstmismatch.second << "\n";
std::cout << "First match in bar is " << *firstmatch.second << "\n";
return 0;
}
Output:
First mismatch in bar is 0
First match in bar is 30
See also
- not2
- Return negation of binary function object (function template)
- unary_negate
- Negate unary function object class (class template)
- binary_function
- Binary function object base class (class template)